# 新浪财经
import requests
import common.dataBase as db


def get_data(html_url):
    headers = {
        'Referer': 'https://finance.sina.com.cn/futures/quotes/XAU.shtml',
        'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/100.0.4896.75 Safari/537.36 Edg/100.0.1185.36'
    }
    response = requests.get(url=html_url, headers=headers)
    return response.text


def app(url, n):
    data = get_data(url)
    hq_str_hf_XAU = data[19:-3]
    hq = hq_str_hf_XAU.split(',')
    if n == 0:
        insert_sql = f'INSERT INTO mat_price VALUES (null, "{hq[-1]}", "{hq[0]}", "{hq[5]}-{hq[4]}", "{hq[-2]}", "{url}")'
    else:
        print(hq)
        insert_sql = f'INSERT INTO mat_price VALUES (null, "{hq[9]}", "{hq[8]}", "{hq[7]}-{hq[6]}", "{hq[-1]}", "{url}")'
    print(insert_sql)
    sql = [insert_sql]
    db.save(sql)


if __name__ == '__main__':
    # app('https://hq.sinajs.cn/?_=1649742265889/&list=hf_XAU', 0)
    app('https://hq.sinajs.cn/rn=1649810298414list=fx_susdcny', 1)
